package logic

import (
	"context"
	"errors"
	"fmt"
	"hm-zero-mall/apps/user/rpc/pb"

	"hm-zero-mall/apps/api/api/internal/svc"
	"hm-zero-mall/apps/api/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type DetailLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DetailLogic {
	return &DetailLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// Detail 获取用户详情
func (l *DetailLogic) Detail() (resp *types.UserInfo, err error) {
	val := l.ctx.Value("userId")
	if val == nil {
		return nil, fmt.Errorf("userId not found in context")
	}
	userID := val.(int64)
	if userID == 0 {
		return nil, errors.New("userId is blank in context")
	}

	user, err := l.svcCtx.UserRPC.GetUserInfo(l.ctx, &pb.UserIdRequest{UserId: userID})
	if err != nil {
		return nil, err
	}

	return &types.UserInfo{
		ID:       user.Id,
		Phone:    user.Phone,
		Username: user.Username,
	}, err
}
